<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="设备编号" prop="innerCode">
        <el-input v-model="queryParams.innerCode" placeholder="请输入设备编号" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="vmList">
      <el-table-column label="序号" type="index" width="100" align="center" prop="id" />
      <el-table-column label="设备编号" align="center" prop="innerCode" />
      <el-table-column label="设备型号" align="center" prop="vmTypeId">
        <template #default="scope">
          <div v-for="item in vmTypeList" :key="item.id">
            <span v-if="item.id == scope.row.vmTypeId">{{ item.name }}</span>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="详细地址" align="center" prop="addr" show-overflow-tooltip />
      <el-table-column label="运营状态" align="center" prop="vmStatus">
        <template #default="scope">
          <dict-tag :options="vm_status" :value="scope.row.vmStatus" />
        </template>
      </el-table-column>
      <el-table-column label="设备状态" align="center" prop="runningStatus">
        <template #default="scope">
          {{ scope.row.runningStatus !== null && JSON.parse(scope.row.runningStatus).status === true ? '正常' : '异常' }}
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" @click="queryVm(scope.row)" v-hasPermi="['manage:vm:query']">查看</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize" @pagination="getList" />

    <!-- 设备详情信息 -->
    <el-dialog :title="title" v-model="showVm" width="500px" append-to-body>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup lang="ts" name="Vm">
  import { listVm, getVm } from '@/api/manage/vm';
  import { listVmType } from '@/api/manage/vmType';
  import { listPartner } from '@/api/manage/partner';
  import { listNode } from '@/api/manage/node';
  import { listRegion } from '@/api/manage/region';
  import { pageQuery } from '@/api/page';
  import { getCurrentInstance, onMounted, reactive, ref, toRefs } from 'vue';
  const { proxy } = getCurrentInstance() as any;
  const { vm_status } = proxy.useDict('vm_status');

  const vmList = ref<any[]>([]);
  const showVm = ref(false);
  const loading = ref(true);
  const showSearch = ref(true);
  const total = ref(0);
  const title = ref('');

  const data = reactive({
    form: {} as any,
    queryParams: {
      pageNum: 1,
      pageSize: 10,
      innerCode: null,
      nodeId: null,
      businessType: null,
      regionId: null,
      partnerId: null,
      vmTypeId: null,
      vmStatus: null,
      runningStatus: null,
      policyId: null
    }
  });

  const { queryParams, form } = toRefs(data);
  /** 查询设备管理列表 */
  function getList() {
    listVm(queryParams.value).then((response: any) => {
      vmList.value = response.rows;
      total.value = response.total;
    });
  }

  /* 查询设备类型列表 */
  const vmTypeList = ref<any[]>([]);
  function getVmTypeList() {
    listVmType(pageQuery).then((response: any) => {
      vmTypeList.value = response.rows;
    });
  }
  /* 查询合作商列表 */
  const partnerList = ref<any[]>([]);
  function getPartnerList() {
    listPartner(pageQuery).then((response: any) => {
      partnerList.value = response.rows;
    });
  }
  /* 查询点位列表 */
  const nodeList = ref([]);
  function getNodeList() {
    listNode(pageQuery).then((response: any) => {
      nodeList.value = response.rows;
    });
  }
  /* 查询区域列表 */
  const regionList = ref([]);
  function getRegionList() {
    listRegion(pageQuery).then((response: any) => {
      regionList.value = response.rows;
    });
  }

  // 取消按钮
  function cancel() {
    showVm.value = false;
    reset();
  }

  // 表单重置
  function reset() {
    form.value = {
      id: null,
      innerCode: null,
      channelMaxCapacity: null,
      nodeId: null,
      addr: null,
      lastSupplyTime: null,
      businessType: null,
      regionId: null,
      partnerId: null,
      vmTypeId: null,
      vmStatus: null,
      runningStatus: null,
      longitudes: null,
      latitude: null,
      clientId: null,
      policyId: null,
      createTime: null,
      updateTime: null
    };
    proxy.resetForm('vmRef');
  }

  /** 搜索按钮操作 */
  function handleQuery() {
    queryParams.value.pageNum = 1;
    getList();
  }

  /** 重置按钮操作 */
  function resetQuery() {
    proxy.resetForm('queryRef');
    handleQuery();
  }

  /** 修改按钮操作 */
  function queryVm(row) {
    reset();
    const _id = row.id;
    getVm(_id).then((response) => {
      form.value = response.data;
      showVm.value = true;
      title.value = '设备详情';
    });
  }

  onMounted(() => {
    loading.value = true;
    getVmTypeList();
    getPartnerList();
    getNodeList();
    getRegionList();
    getList();
    loading.value = false;
  });
</script>

<style lang="scss" scoped>
  @import '@/assets/styles/dkd.scss';
</style>
